window.ResMgr = {
    resPool: new Map(),
    //加载资源
    LoadRes: function(path, type, callback) {
        if (ResMgr.resPool.has(path)) {
            return false;
        }
        cc.resources.load(path, type, function (err, prefab) {
            if (err==null) {
                if (ResMgr.resPool.has(path)) {
                    cc.log("loadRes: "+path+" err:already exist");
                    if (callback!=null) {
                        callback(err,path);
                    }
                    return;
                }
                ResMgr.resPool.set(path, {
                    path: path,//资源路径
                    obj: prefab,//资源对象
                    type: type,//资源类型
                    ref: 0//引用计数
                });
            }
            cc.log("loadRes: "+path+" err:"+err);
            if (callback!=null) {
                callback(err,path);
            }
        });
        return true;
    },
    //获取实例
    GetInstance: function(path) {
        let p = this.resPool.get(path);
        if(p){
            p.ref++;
            p.obj.addRef();
            if (p.type==cc.Prefab) {
                return cc.instantiate(p.obj);
            }
            else{
                return p.obj;
            }
        }
        return null;
    },
    GetInstanceArrary: function(path, num) {
        let arr = new Array();
        let p = this.resPool.get(path);
        if(p && num>0){
            p.ref += num;
            for (let i = 0; i < num; i++) {
                if (p.type==cc.Prefab) {
                    arr.push(cc.instantiate(p.obj));
                }
                else{
                    arr.push(p.obj);
                }
                p.obj.addRef();
            }
        }
        return arr;
    },
    //释放实例
    ReleaseInstance: function(path, num=1) {
        let p = this.resPool.get(path);
        if(p){
            p.ref -= num;
            for (let i = 0; i < num; i++) {
                p.obj.decRef();
            }
            if (p.ref <= 0) {
                p.obj = null;
                this.resPool.delete(path);
            }
        }
    },
    //强制释放
    forceRelease: function(path) {
        let p = this.resPool.get(path);
        if(p){
            cc.assetManager.releaseAsset(p.obj);
            p.obj = null;
            this.resPool.delete(path);
        }
    }
};
